package cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.*;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.projectPlan.LiveProjectPlanDO;
import cn.iocoder.yudao.adminserver.modules.live.convert.projectPlan.LiveProjectPlanConvert;
import cn.iocoder.yudao.adminserver.modules.live.service.projectPlan.LiveProjectPlanService;

@Api(tags = "项目阶段计划")
@RestController
@RequestMapping("/live/project-plan")
@Validated
public class LiveProjectPlanController {

    @Resource
    private LiveProjectPlanService projectPlanService;

    @PostMapping("/create")
    @ApiOperation("创建项目阶段计划")
    @PreAuthorize("@ss.hasPermission('live:project-plan:create')")
    public CommonResult<Long> createProjectPlan(@Valid @RequestBody LiveProjectPlanCreateReqVO createReqVO) {
        return success(projectPlanService.createProjectPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目阶段计划")
    @PreAuthorize("@ss.hasPermission('live:project-plan:update')")
    public CommonResult<Boolean> updateProjectPlan(@Valid @RequestBody LiveProjectPlanUpdateReqVO updateReqVO) {
        projectPlanService.updateProjectPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除项目阶段计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('live:project-plan:delete')")
    public CommonResult<Boolean> deleteProjectPlan(@RequestParam("id") Long id) {
        projectPlanService.deleteProjectPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目阶段计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('live:project-plan:query')")
    public CommonResult<LiveProjectPlanRespVO> getProjectPlan(@RequestParam("id") Long id) {
        LiveProjectPlanDO projectPlan = projectPlanService.getProjectPlan(id);
        return success(LiveProjectPlanConvert.INSTANCE.convert(projectPlan));
    }

    @GetMapping("/list")
    @ApiOperation("获得项目阶段计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('live:project-plan:query')")
    public CommonResult<List<LiveProjectPlanRespVO>> getProjectPlanList(@RequestParam("ids") Collection<Long> ids) {
        List<LiveProjectPlanDO> list = projectPlanService.getProjectPlanList(ids);
        return success(LiveProjectPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得项目阶段计划分页")
    @PreAuthorize("@ss.hasPermission('live:project-plan:query')")
    public CommonResult<PageResult<LiveProjectPlanRespVO>> getProjectPlanPage(@Valid LiveProjectPlanPageReqVO pageVO) {
        PageResult<LiveProjectPlanDO> pageResult = projectPlanService.getProjectPlanPage(pageVO);
        return success(LiveProjectPlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出项目阶段计划 Excel")
    @PreAuthorize("@ss.hasPermission('live:project-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportProjectPlanExcel(@Valid LiveProjectPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LiveProjectPlanDO> list = projectPlanService.getProjectPlanList(exportReqVO);
        // 导出 Excel
        List<LiveProjectPlanExcelVO> datas = LiveProjectPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目阶段计划.xls", "数据", LiveProjectPlanExcelVO.class, datas);
    }

}
